<template>
	<view class="bg">
		<top_nav_return_green :topNavTitle="topNavTitle" :router="'pages/index/index'"></top_nav_return_green>
		<!-- 错题搜索 -->
		<view class="search_box" >
			<img src="../../../static/icon/search.png" alt="">
			<input type="text" placeholder="搜索收藏关键词" v-model="searchText">
			<view class="search_box_btn" @click="get_listError(num_a,'search')"> 搜索 </view>
		</view>
		<!-- 导航栏 -->
		<view class="tabber_box" >
			<view class="tabber_item" :class="{active_tabber_item : num_a == 0}" @tap="num_a_change(0)"> 全部 </view>
			<view class="tabber_item" :class="{active_tabber_item : num_a == item.liftingType}"
				@tap="num_a_change(item.liftingType)" v-for="(item,index) in typeList" :key="index">
				<span v-if="item.liftingType == 1">单选题</span>
				<span v-else-if="item.liftingType == 2">多选题</span>
				<span v-else-if="item.liftingType == 3">共用题干</span>
				<span v-else-if="item.liftingType == 4">共用选项</span>
			</view>
		</view>
		<!-- 错题列表 -->
		<view class="list_box" v-if="list.length>0">
			<view class="list" v-for="(item,index) in list" :key="index">
				<view class="list_content" @click.stop="queryStudentBySubjectId(item)">
					<view class="list_content_No">
						<span v-if="item.liftingType == 1">单选题</span>
						<span v-else-if="item.liftingType == 2">多选题</span>
						<span v-else-if="item.liftingType == 3">共用题干</span>
						<span v-else-if="item.liftingType == 4">共用选项</span>
					</view>
					{{item.stem}}
				</view>
				<view class="list_text">
					<view class="list_time">
						做错:<span style="color:#0492A1;"> {{item.number}}</span>次
						{{item.createTime != null ? item.createTime : '' }}
					</view>
					<view class="list_btn">
						<img src="../../../static/icon/collection_ed.png" alt="" v-if="item.isCollect == 1"
							@click.stop="active_collection(item)">
						<img src="../../../static/icon/before_collection.png" alt="" v-else-if="item.isCollect == 0"
							@click.stop="active_collection(item)">
						<img src="../../../static/icon/del_green.png" alt="" @click.stop="removeQuestionError(item)">
					</view>
				</view>
			</view>
		</view>
		<view class="censiw" v-else-if="list.length==0 && isAss">
			<image src="../../../static/xing/sw.png" class="zaws" mode=""></image>
			<view class="zanwubiji">
				暂无错题
			</view>
		</view>
		<view class="list_end" v-if="agree_loding == false &&list.length>0"> —— 到底了 —— </view>
		<view class="xmct_btn" @click="go_kill()" v-if="list.length>0">
			消灭错题
		</view>
	</view>
</template>

<script>
	import {
		findByType, // 错题类型
		listError, // 错题列表
		removeQuestionError, //移除错题
	} from '@/api/nav/cuoti.js'
	import {
		delete_collection, // 取消收藏
		add_collection, // 添加收藏
	} from '@/api/nav/shoucang.js'
	import {
		queryStudentBySubjectId, //查看原题
	} from '@/api/nav/biji.js'
	import {
		getToken,
		getMajorInfo,
		getUserInfo
	} from '@/common/util/index.js'
	export default {
		data() {
			return {
				topNavTitle: '我的错题',
				searchText: '',
				pageNo: 1,
				pageSize: 10,
				typeList: [],
				list: [],
				isAss: false,
				num_a: 0,
				imageUrls: getApp().globalData.imaeWd,
				agree_loding: false
			}
		},
		onShow() {
			console.log('onshow');
			this.list = []
			//	查询错题列表
			// this.num_a = 0
			//	获取错题类型
			this.get_findByType()
			//  获取错题列表
			if (this.num_a != 0) {
				this.get_listError(this.num_a, 'search')
			} else {
				this.get_listError(0) // 进入页面默认用0获取全部类型的数据
			}
		},
		onReachBottom() {
			console.log(123)
			if (this.agree_loding == true) {
				this.pageNo++
				this.get_listError(this.num_a)
			}
		},
		methods: {
			queryStudentBySubjectId(item) {
				console.log(item, 'ww');
				queryStudentBySubjectId({
					majorId: getMajorInfo().id,
					subjectId: item.id,
					type: 0, //0错题1其他
				}).then((res) => {
					uni.navigateTo({
						url: `/pagesB/parsing/index?sourceType=99&sourType=1&couti=1&isAss=true&listArr=${encodeURIComponent(JSON.stringify( res.result.list))}`
					})
					// uni.reLaunch({
					// 	url: `/pagesB/parsing/index?sourceType=3&sourType=${this.num_model}&listArr=${encodeURIComponent(JSON.stringify( res.result.list))}`
					// })
					console.log('错题查询原题：', res);
				})
			},
			active_collection(recieve) {
				let wdQuestionCollect = {
					agencyId: getMajorInfo().agencyId,
					createTime: '2022-10-10 12:00:00',
					id: '',
					majorId: getMajorInfo().id,
					studentId: getUserInfo().id,
					subjectId: recieve.id, //题干id
				}
				console.log('错题添加收藏请求数据：', wdQuestionCollect);
				add_collection(wdQuestionCollect).then((res) => {
					console.log('错题添加收藏返回值:', res);
					if (res.code == 200) {
						uni.showToast({
							title: res.message
						})
						if (res.message == "添加成功") {
							let index = this.list.findIndex((item, i) => {
								// console.log('pp', item.id, '--oo', recieve.id);
								return item.id == recieve.id
							})
							this.list[index].isCollect = 1
						} else if (res.message == '取消成功') {
							let index = this.list.findIndex((item, i) => {
								// console.log('uu', item.id, '--yy', recieve.id);
								return item.id == recieve.id
							})
							this.list[index].isCollect = 0
						} else {
							console.log('意外错误拉aa');
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: 'error'
						})
					}
				})
			},
			removeQuestionError(recieve) {
				let that = this
				uni.showModal({
					title: '移除错题',
					success: function(res) {
						if (res.confirm) {
							removeQuestionError({
								majorId: getMajorInfo().id,
								subjectId: recieve.id, //题干id
							}).then((res) => {
								console.log('移除错题返回值:', res);
								if (res.code == 200) {
									uni.showToast({
										title: '移除错题成功'
									})
									let index = that.list.findIndex((item, i) => {
										return item.id == recieve.id
									})
									that.list.splice(index, 1)
								} else {
									uni.showToast({
										title: '移除错题失败',
										icon: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			go_kill() {
				// 智能组题[错题]，消灭错题
				uni.navigateTo({
					url: '/pagesA/nav/aiQuestionGroup/aiQuestionGroup?agreeChangeFrom=false&num_from=3'
				})
			},
			num_a_change(e) {
				this.num_a = e
				this.get_listError(e, 'search')
			},
			get_listError(liftingType, text) {
				if (text == 'search') {
					this.agree_loding=false
					console.log('搜索关键字、筛选题型');
					this.pageNo = 1
					if (liftingType == 0) {
						listError({
							agencyId: getMajorInfo().agencyId,
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							studentId: getUserInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							topicCategory: ''
						}).then((res) => {
							this.list = []
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							if (res.result.records.length == this.pageSize) {
								console.log('全题型搜索', this.agree_loding);
								this.agree_loding = true
							} else {
								console.log('全题型搜索2', this.agree_loding);
								this.agree_loding = false
							}
						})
					} else {
						listError({
							agencyId: getMajorInfo().agencyId,
							liftingType: liftingType,
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							studentId: getUserInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							topicCategory: ''
						}).then((res) => {
							this.list = []
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('单题型搜索', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					}
				} else {
					console.log('进入页面，触底加载');
					if (liftingType == 0) {
						listError({
							agencyId: getMajorInfo().agencyId,
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							studentId: getUserInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							topicCategory: ''
						}).then((res) => {
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('e', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					} else {
						let data = {
							agencyId: getMajorInfo().agencyId,
							liftingType: liftingType,
							keyWord: this.searchText,
							majorId: getMajorInfo().id,
							studentId: getUserInfo().id,
							pageNo: this.pageNo,
							pageSize: this.pageSize,
							topicCategory: ''
						}
						console.log('reqdata:', data);
						listError(data).then((res) => {
							// console.log('收藏列表获取：', res);
							this.list = this.list.concat(res.result.records)
							this.isAss = true
							console.log('f', this.list);
							if (res.result.records.length == this.pageSize) {
								this.agree_loding = true
							} else {
								this.agree_loding = false
							}
						})
					}
				}
			},
			get_findByType() {
				findByType({
					majorId: getMajorInfo().id,
				}).then((res) => {
					console.log('错题类型返回', res);
					let list = res.result
					let arr = list.filter((item) => item != null)
					arr.forEach((item, index) => {
						item.liftingType = Number(item.liftingType)
					})
					var compare = function(prop) {
						return function(obj1, obj2) {
							var val1 = obj1[prop];
							var val2 = obj2[prop];
							if (val1 < val2) {
								return -1;
							} else if (val1 > val2) {
								return 1;
							} else {
								return 0;
							}
						}
					}
					arr.sort(compare('liftingType'))
					console.log(arr, '类型排序');
					this.typeList = arr
				})
			},
			backPage() {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.censiw {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
	}

	.zaws {
		width: 600rpx;
		height: 600rpx;
	}

	.zanwubiji {
		font-size: 32rpx;
		font-weight: 500;
		color: rgba(102, 102, 102, 0.5);
		text-align: center;
	}

	.list_end {
		padding: 50rpx 0;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		color: #ccc;
		font-size: 30rpx;
	}

	.xmct_btn {
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		margin-left: -250rpx;
		width: 500rpx;
		height: 82rpx;
		text-align: center;
		line-height: 82rpx;
		background: #0191A0;
		border-radius: 40rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.list_box {
		margin: 0 30rpx;

		.list {
			background-color: white;
			padding: 30rpx 30rpx;
			margin-bottom: 30rpx;
			border-radius: 20rpx;


		}
	}

	.list_content {

		.list_content_No {
			display: inline-block;
			margin-right: 30rpx;
			width: 98rpx;
			height: 38rpx;
			line-height: 38rpx;
			text-align: center;
			align-items: center;
			background: #edf7f8;
			border-radius: 10rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #0492A1
		}
	}

	.list_text {
		margin-top: 33rpx;
		display: flex;
		justify-content: space-between;
		font-size: 24rpx;
		font-weight: 400;
		color: #999999;

		.list_time {}

		.list_btn {
			display: flex;
			align-items: center;

			:last-child {
				margin-right: 0 !important;
			}

			img {
				margin-right: 40rpx;
				width: 26rpx;
				height: 26rpx;
			}
		}
	}

	.tabber_box {
		display: flex;
		margin: 33rpx 30rpx;

		.tabber_item {
			margin-right: 10rpx;
			width: 135px;
			height: 51rpx;
			line-height: 51rpx;
			text-align: center;
			background: #EEEEEE;
			border-radius: 26rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #999999;
		}
	}

	.search_box {
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 30rpx;
		margin-top: 30rpx;
		padding-left: 22rpx;
		padding-right: 10rpx;
		width: 690rpx;
		height: 70rpx;
		background: #FFFFFF;
		border: 1rpx solid #0191A0;
		border-radius: 35rpx;
		background: #fff;

		input {
			width: 70%;
		}

		img {
			width: 34rpx;
			height: 34rpx;
			margin-right: 20rpx;
		}

		.search_box_btn {
			width: 121rpx;
			height: 51rpx;
			line-height: 51rpx;
			text-align: center;
			background: #0191A0;
			border-radius: 26rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}



	.bg {
		padding-top: 160rpx;
		padding-bottom: 140rpx;
		background: rgba(249, 249, 249, 0.8);
		min-height: 100vh;

	}

	// --------------------------------------- 动态样式
	.active_tabber_item {
		width: 135px;
		height: 51rpx;
		line-height: 51rpx;
		text-align: center;
		background: #0191A0 !important;
		border-radius: 26rpx;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFFFFF !important;
	}
</style>
